#!/usr/bin python2
# coding=utf-8

import os
import numpy as np
import matplotlib.pyplot as plt
from sklearn import svm

'''
a function, load study files
parameter: path of those study samples
return vector and label
'''
def loadData(path='./imagedata/'):
    files = os.listdir(path) 
    data = []
    label = []
    for f in files:
        fp = open(path+f)
        name = f.split('_')[0]
        imgdata = []
        label.append(name)
        for i in range(32):
            lineStr = fp.readline()
            for j in range(32):
                imgdata.append(lineStr[j])
        data.append(imgdata)
        fp.close()
    returnVect = np.array(data)
    return returnVect, label 

def main(file):
    arg = '../PyQt/txt_cache/'+file
    fp = open(arg)
    testdata = []
    imgdata = []
    for i in range(32):
        lineStr = fp.readline()
        for j in range(32):
            imgdata.append(lineStr[j])
    testdata.append(imgdata)
    fp.close()
    classifier = svm.NuSVC(gamma=0.0001)
    x, y = loadData()
    n = len(x)
    classifier.fit(x, y)
    predicted = classifier.predict(testdata)
    print file + ' = ' + str(predicted)

if __name__ == '__main__':
    #fp = open('DIVISIONED_1.txt')
    txt = os.listdir('../PyQt/txt_cache/')
    i = 0
    for t in txt:
        i += 1
        main(t)
    print i